/**
 * A simple class based on Uploader from Mr. Hillah. Used to downaload multiple files from FTP server to a selected directory.
 * 
 * @author tcharbon
 */

package simpleftp.client.control;

import java.io.File;
import java.util.concurrent.Callable;

import simpleftp.client.gui.FTPClientWindow;

public final class Downloader implements Callable<Boolean> {
	private SimpleFTP client;
	private String server;
	private String port;
	private FTPClientWindow wind;
	private String user;
	private String pass;
	public File file;
	public File receiverDir;

	/**
	 * Create the Downloader
	 * @param f           The File to download from the FTP server.
	 * @param receiverDir The directory where the downloaded File will be created.
	 * @param window
	 * @param server
	 * @param port
	 * @param user
	 * @param pass
	 */
	public Downloader(File f, File receiverDir, FTPClientWindow window, String server, String port, String user, String pass) {
		this.file = f;
		this.server = server;
		this.port = port;
		this.wind = window;
		this.user = user;
		this.pass = pass;
		this.receiverDir = receiverDir;
		client = new SimpleFTP();
	}

	@Override
	public Boolean call() throws Exception {
		System.out.println("Download starting for " + file.getName());
		Boolean result = false;
		client.connect(server, Integer.parseInt(port), user, pass);
		result = client.download(file.getName(), receiverDir);
		if (result) {
			wind.console.append("\nSuccessfully downloaded file " + file.getName());
			System.out.println("Successfully downloaded file " + file.getName());
		} else {
			wind.console.append("\nFailed downloading file " + file.getName());
			System.out.println("Failed downloading file " + file.getName());
		}
		return result;
	}
}
